package cn.com.czpb.client.module.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import cn.com.czpb.client.R;
import cn.com.czpb.client.api.InterfaceJsonfile;
import cn.com.czpb.client.utils.LogUtils;
import cn.com.czpb.client.utils.SPUtil;
import cn.com.czpb.client.utils.StringUtils;
import cn.com.czpb.client.utils.TUtils;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.favorite.WechatFavorite;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * Created by Administrator on 2017/10/21.
 */

public class ShareDialog extends Dialog implements View.OnClickListener {
    private ShareDialog shareDialog;


    private Activity activity;
    private View view;
    private LinearLayout ll_wxhy;
    private LinearLayout ll_wxsc;
    private LinearLayout ll_wxpyq;
    private LinearLayout ll_qqkj;
    private LinearLayout ll_xlwb;
    private LinearLayout ll_qqhy;
    private ImageView iv_cancle;
    private ImageView iv_code;
    private SPUtil spu;
    private static int rtype;
    private String link;
    private String imgurl;
    private String title;
    private String sharelogo;

    private static Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (112 == msg.what) {
                TUtils.toast("分享失败");
            } else if (113 == msg.what) {
                TUtils.toast("分享成功");
            } else if (114 == msg.what) {
//                TUtils.toast("分享取消");
            }
        }
    };

    public ShareDialog(@NonNull Context context) {
        super(context);
        this.activity = (Activity) context;
        init();
    }

//    public ShareDialog(@NonNull Context context, @StyleRes int themeResId, NewsDetailBean nb, int type) {
//        super(context, themeResId);
//        this.activity = (Activity) context;
//        this.nb = nb;
//        this.rtype = type;
//        init();
//    }

    public ShareDialog(@NonNull Context context, @StyleRes int themeResId,String title,String link,String imgurl, int type) {
        super(context, themeResId);
        this.activity = (Activity) context;
        this.title =title;
        this.link = link;
        this.imgurl = imgurl;
        this.rtype = type;
        init();
    }

    public ShareDialog(@NonNull Context context, @StyleRes int themeResId,String title,String link,String imgurl, int type,String sharelogo) {
        super(context, themeResId);
        this.activity = (Activity) context;
        this.title =title;
        this.link = link;
        this.imgurl = imgurl;
        this.rtype = type;
        this.sharelogo = sharelogo;
        init();
    }


    public void init() {
        WindowManager wm = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        LogUtils.i("width-->" + width);
        view = LayoutInflater.from(activity).inflate(R.layout.dialog_share, null);

        ll_qqhy = view.findViewById(R.id.ll_qqhy);
        ll_qqkj = view.findViewById(R.id.ll_qqkj);
        ll_xlwb = view.findViewById(R.id.ll_xlwb);
        ll_wxhy = view.findViewById(R.id.ll_wxhy);
        ll_wxpyq = view.findViewById(R.id.ll_wxpyq);
        ll_wxsc = view.findViewById(R.id.ll_wxsc);
        iv_cancle = view.findViewById(R.id.iv_cancle);
        iv_code = view.findViewById(R.id.iv_code);

        ll_qqhy.setOnClickListener(this);
        ll_qqkj.setOnClickListener(this);
        ll_xlwb.setOnClickListener(this);
        ll_wxhy.setOnClickListener(this);
        ll_wxpyq.setOnClickListener(this);
        ll_wxsc.setOnClickListener(this);
        iv_cancle.setOnClickListener(this);
        setContentView(view);
        if (rtype==0){
            iv_code.setVisibility(View.VISIBLE);
        }else {
            iv_code.setVisibility(View.GONE);
        }
    }

    public void showShare(String type, Context context) {

        OnekeyShare oks = new OnekeyShare();
        if (!StringUtils.isEmpty(imgurl)) {
            oks.setImageUrl(imgurl);
        } else if(!StringUtils.isEmpty(sharelogo)){
//            oks.setImagePath(sharelogo);
            oks.setImageUrl(InterfaceJsonfile.PATH_ROOT_SHARE);
        }

        LogUtils.i("link-->"+link);
        LogUtils.i("img-->"+imgurl);
        LogUtils.i("title-->"+title);
        oks.setTitleUrl(link);
        oks.setText("广西云");
        oks.setTitle(title);
        oks.setUrl(link);// 分享内容的url、在微信和易信中也使用为视频文件地址
        if(null!=imgurl){
            oks.setImageUrl(imgurl);//待分享的网络图片
        }else{
//			oks.setImageUrl(context.getString(R.string.shared_img));
            oks.setImageUrl(InterfaceJsonfile.PATH_ROOT_SHARE);
        }
        oks.setPlatform(type);

        oks.setCallback(new PlatformActionListener() {
            @Override
            public void onError(Platform arg0, int arg1, Throwable arg2) {
                handler.sendEmptyMessage(112);
            }
            @Override
            public void onComplete(Platform arg0, int arg1, HashMap<String, Object> arg2) {
                handler.sendEmptyMessage(113);
            }

            @Override
            public void onCancel(Platform arg0, int arg1) {
                handler.sendEmptyMessage(114);
            }
        });
        // 在自动授权时可以禁用SSO方式
        oks.disableSSOWhenAuthorize();
        oks.show(context);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_qqhy://aa好友
                showShare(QQ.NAME, activity);
                dismiss();
                break;
            case R.id.ll_qqkj://qq空间
                showShare(QZone.NAME, activity);
                dismiss();
                break;
            case R.id.ll_xlwb://新浪微博
                showShare(SinaWeibo.NAME, activity);
                dismiss();
                break;
            case R.id.ll_wxhy://微信好友
                showShare(Wechat.NAME, activity);
                dismiss();
                break;
            case R.id.ll_wxpyq://微信朋友圈
                showShare(WechatMoments.NAME, activity);
                dismiss();
                break;
            case R.id.ll_wxsc://微信收藏
                showShare(WechatFavorite.NAME,activity);
                dismiss();
                break;
            case R.id.iv_cancle:
                dismiss();
                break;
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        handler.removeCallbacks(null);
    }
}
